package com.wangzhixuan.apiController;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.scan.MyException;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.model.AnalysisMethod;
import com.wangzhixuan.model.AnalysisType;
import com.wangzhixuan.service.IAnalysisMethodService;
import com.wangzhixuan.service.IAnalysisTypeService;

/**
 * <p>
 *   前端控制器
 * </p>
 * @author zhixuan.wang
 * @since 2017-07-16
 */
@Controller
@RequestMapping("/api")
public class AnalysisApiController extends ApiBaseController {
    
    @Autowired private IAnalysisTypeService analysisTypeService;
    
    @Autowired private IAnalysisMethodService analysisMethodService;
    
    /**
     * 获取问题现象列表
     * @return
     */
    @RequestMapping("/phenomenon/list")
    @ResponseBody
    public Object phenomenonList() {
    	List<Map<String,Object>> retList = analysisTypeService.selectByPid(0);	
        return renderSuccess(ApiCode.CODE_200, "成功",retList);
    }
    
    /**
     * 获取问题原因列表
     * @param ponId
     * @return
     * @throws MyException 
     */
    @RequestMapping("/reason/list")
    @ResponseBody
    public Object reasonList(Integer ponId) throws MyException {
    	if(ponId==null){
			 throw new MyException(ApiCode.CODE_501, "请选择原因");
		}
    	List<Map<String,Object>> retList = analysisTypeService.selectByPonId(ponId);	
        return renderSuccess(ApiCode.CODE_200, "成功",retList);
    }
    
    /**
     * 添加问题现象
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/phenomenon/add")
    @ResponseBody
    public Object phenomenonAdd(String ponDetails,Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	if (StringUtils.isBlank(ponDetails)) {
            return renderError(ApiCode.CODE_501, "请选择现象原因");
        }
    	Map<String,Object> columnMap = new HashMap<String,Object>();
        columnMap.put("name", ponDetails);
        columnMap.put("pid", 0);
    	List<AnalysisType> list = analysisTypeService.selectByMap(columnMap);
    	if(list!=null && list.size()>0){
    		return renderError(ApiCode.CODE_500, "已存在不能重复添加");
    	}
    	AnalysisType analysisType = new AnalysisType();
    	analysisType.setName(ponDetails);
    	analysisType.setPid(0);
    	boolean b = analysisTypeService.insert(analysisType);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "添加失败");
        }
    }
    
    /**
     * 添加问题原因
     * @param ponDetails
     * @param ponId
     * @param userId
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws MyException 
     */
    @RequestMapping("/reason/add")
    @ResponseBody
    public Object reasonAdd(String ronDetails,Integer ponId, Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	if(ponId==null){
			 throw new MyException(ApiCode.CODE_501, "ponId不能为空");
		}
    	if (StringUtils.isBlank(ronDetails)) {
           return renderError(ApiCode.CODE_501, "请选择问题原因");
        }
    	Map<String,Object> columnMap = new HashMap<String,Object>();
        columnMap.put("name", ronDetails);
        columnMap.put("pid", ponId);
    	List<AnalysisType> list = analysisTypeService.selectByMap(columnMap);
    	if(list!=null && list.size()>0){
    		return renderError(ApiCode.CODE_500, "已存在不能重复添加");
    	}
    	AnalysisType analysisType = new AnalysisType();
    	analysisType.setName(ronDetails);
    	analysisType.setPid(ponId);
    	boolean b = analysisTypeService.insert(analysisType);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "添加失败");
        }
    }

    /**
     * 修改问题现象
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/phenomenon/edit")
    @ResponseBody
    public Object phenomenonEdit(String ponDetails,Integer ponId, Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	if(ponId==null){
			 throw new MyException(ApiCode.CODE_501, "请选择问题现象");
		}
    	if (StringUtils.isBlank(ponDetails)) {
    		return renderError(ApiCode.CODE_501, "请选择问题现象");
        }
    	
    	AnalysisType analysisType = new AnalysisType();
    	analysisType.setName(ponDetails);
    	analysisType.setId(ponId);
    	boolean b = analysisTypeService.updateById(analysisType);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "编辑失败");
        }
    }
    
    @RequestMapping("/reason/edit")
    @ResponseBody
    public Object reasonEdit(String ronDetails,Integer ronId, Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	if(ronId==null){
			 throw new MyException(ApiCode.CODE_501, "请选择问题原因");
		}
    	if (StringUtils.isBlank(ronDetails)) {
    		return renderError(ApiCode.CODE_501, "请选择问题原因");
        }
    	AnalysisType analysisType = new AnalysisType();
    	analysisType.setName(ronDetails);
    	analysisType.setId(ronId);
    	boolean b = analysisTypeService.updateById(analysisType);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "编辑失败");
        }
    }
    
    /**
     * 删除问题现象
     * @param id
     * @return
     * @throws MyException 
     */
    @RequestMapping("/phenomenon/delete")
    @ResponseBody
    public Object phenomenonDelete(Integer ponId) throws MyException {
    	if(ponId==null){
			 throw new MyException(ApiCode.CODE_501, "请选择问题现象");
		}
        boolean b = analysisTypeService.deleteById(ponId);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "删除失败");
        }
    }
    
    /**
     * 删除问题原因
     * @param ronId
     * @return
     * @throws MyException 
     */
    @RequestMapping("/reason/delete")
    @ResponseBody
    public Object reasonDelete(Integer ronId) throws MyException {
    	if(ronId==null){
			 throw new MyException(ApiCode.CODE_501, "请选择问题原因");
		}
        boolean b = analysisTypeService.deleteById(ronId);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "删除失败");
        }
    }
    
   //////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 添加
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/method/add")
    @ResponseBody
    public Object methodadd(Integer ronId,String modDetails,Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
		if (userId == null) {
			throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
		if (ronId == null) {
			throw new MyException(ApiCode.CODE_501, "请选择问题原因");
		}
		if (StringUtils.isBlank(modDetails)) {
			return renderError(ApiCode.CODE_501, "请选择解决方法");
		}
    	Map<String,Object> columnMap = new HashMap<String,Object>();
        columnMap.put("name", modDetails);
        columnMap.put("type_id", ronId);
    	List<AnalysisMethod> list = analysisMethodService.selectByMap(columnMap);
    	if(list!=null && list.size()>0){
    		return renderError(ApiCode.CODE_500, "已存在不能重复添加");
    	}
    	AnalysisMethod analysisMethod = new AnalysisMethod();
    	analysisMethod.setName(modDetails);
    	analysisMethod.setTypeId(ronId);
    	analysisMethod.setShowIndex(0);
    	boolean b = analysisMethodService.insert(analysisMethod);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "添加失败");
        }
    }
    
    /**
     * 修改问题方法
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/method/edit")
    @ResponseBody
    public Object editMethod(Integer modId,String modDetails,Integer userId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if (userId == null) {
			throw new MyException(ApiCode.CODE_501, "请重新登陆");
		}
		if (modId == null) {
			throw new MyException(ApiCode.CODE_501, "请选择解决方法");
		}
		if (StringUtils.isBlank(modDetails)) {
			return renderError(ApiCode.CODE_501, "请选择解决方法");
		}
    	AnalysisMethod analysisMethod = new AnalysisMethod();
        analysisMethod.setName(modDetails);
        analysisMethod.setId(modId);
    	boolean b = analysisMethodService.updateById(analysisMethod);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "编辑失败");
        }
    }

    /**
     * 删除问题方法
     * @param id
     * @return
     * @throws MyException 
     */
    @RequestMapping("/method/delete")
    @ResponseBody
    public Object deleteMethod(Integer modId) throws MyException {
    	if (modId == null) {
			throw new MyException(ApiCode.CODE_501, "modId不能为空");
		}
        boolean b = analysisMethodService.deleteById(modId);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "删除失败");
        }
    }
    
    @RequestMapping("/method/list")
    @ResponseBody
    public Object methodList(Integer ronId) throws MyException {
    	if (ronId == null) {
			throw new MyException(ApiCode.CODE_501, "请选择解决方法");
		}
    	List<Map<String,Object>> retList = analysisMethodService.selectByRonId(ronId);	
        return renderSuccess(ApiCode.CODE_200, "成功",retList);
    }

}
